package com.rabbitmq.directdemo.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class DirectConfig {

    public static final String DIRECT_QUEUE="direct_queue";
    public static final String DIRECT_EXCHANGE="direct_exchange";
    public static final String DIRECT_ROUTE_KEY="direct_route_key";

    /**
     * 创建队列
     */
    @Bean
    public Queue queue01(){
        //                      队列名             是否持久化     是否独占          是都自动删除
        return new Queue(DIRECT_QUEUE+"_01",false,false,true);
    }

    @Bean
    public Queue queue02(){
        //还可以有跟多属性，详情见源码 ， 此处只是多展示依照那个创建方式
//        return QueueBuilder.nonDurable(FANOUT_QUEUE+"_02").autoDelete().exclusive().build();
        return QueueBuilder.nonDurable(DIRECT_QUEUE+"_02").autoDelete().build();
    }
    @Bean
    public Queue queue03(){
        return QueueBuilder.nonDurable(DIRECT_QUEUE+"_03").autoDelete().build();
    }

    /**
     * 创建的交换机
     */
    @Bean
    public DirectExchange DirectExchange(){
        //同理队列，建造者模式也可以在后面加很多属性
        return ExchangeBuilder.directExchange(DIRECT_EXCHANGE).autoDelete().build();
    }

    /**
     * 绑定关系
     */
    @Bean
    public Binding fanout01(){
        return BindingBuilder.bind(queue01()).to(DirectExchange()).with(DIRECT_ROUTE_KEY+"_01");
    }
    @Bean
    public Binding fanout02(){
        return BindingBuilder.bind(queue02()).to(DirectExchange()).with(DIRECT_ROUTE_KEY+"_02");
    }
    @Bean
    public Binding fanout03key01(){
        return BindingBuilder.bind(queue03()).to(DirectExchange()).with(DIRECT_ROUTE_KEY+"_01");
    }
    @Bean
    public Binding fanout03key02(){
        return BindingBuilder.bind(queue03()).to(DirectExchange()).with(DIRECT_ROUTE_KEY+"_02");
    }
}
